﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Enterprise.Management.Common;
using Enterprise.Management.FilterAttributes;
using Enterprise.Management.Data;
using Enterprise.Management.Caches;
using Enterprise.Management.Business;


namespace Enterprise.Management.Controllers
{
    [Login]
    public class BaseController : Controller
    {
        public static string GetHomeUrl(FunctionType type)
        {
            string controller = "Home", action = "Index";
            switch (type)
            { 
                case FunctionType.Audit:
                    controller = "Audit";
                    break;
                case FunctionType.BI:
                    controller = "BI";
                    break;
                case FunctionType.ContractManagement:
                    controller = "Contract";
                    break;
                case FunctionType.DependencyBranch:
                    controller = "DependencyBranch";
                    break;
                case FunctionType.DependencyEconomy:
                    controller = "DependencyEconomy";
                    break;
                case FunctionType.HR:
                    controller = "HR";
                    break;
                case FunctionType.InternalManagement:
                    controller = "Internal";
                    break;
                case FunctionType.LisenceManagement:
                    controller = "Lisence";
                    break;
                case FunctionType.RewardsPunishment:
                    controller = "RewardsPunishment";
                    break;
            }

            return UrlBuilder.GenerateUrl(action, controller);
        }

        protected override void HandleUnknownAction(string actionName)
        {
            Log.Error(string.Format("access unknown page: {0}", actionName));

            new RedirectToRouteResult("Default", new RouteValueDictionary(new
            {
                controller = "Error",
                action = "Index",
                errorView = "404"
            })).ExecuteResult(this.ControllerContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            Log.Error(filterContext.Exception);

            if (!string.IsNullOrEmpty(filterContext.HttpContext.Request["ajax"]))
                return;

            filterContext.ExceptionHandled = true;
            filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary(new
            {
                controller = "Error",
                action = "Index",
                errorView = "UnknownError"
            }));
        }
    }
}
